SS.Smartness = SS.Plugins:New("Smartness")

// Config

include("Config.lua")

// Check smartness

function SS.Smartness.Check(Player)
	if SS.Smartness.Ban then
		if CVAR.Request(Player, "Smartness") <= SS.Smartness.Points then
			CVAR.Update(Player, "Smartness", SS.Smartness.Start)
			
			SS.Lib.PlayerBan(Player, SS.Smartness.Time, "Smartness is too low!", true)
		end
	end
end

// Take smartness

function SS.Smartness.Take(Player, Amount, Reason)
	umsg.Start("Smartness.Lose", Player)
		umsg.Short(Amount)
		umsg.String(Reason)
	umsg.End()
	
	SS.Player.PlayerUpdateGUI(Player)
end

------[ GIVE SMARTNESS ]---------

function SS.Smartness.Give(Player, Amount, Reason)
	umsg.Start("Smartness.Gain", Player)
		umsg.Short(Amount)
		umsg.String(Reason)
	umsg.End()
	
	SS.Player.PlayerUpdateGUI(Player)
end

// When players variables are set

function SS.Smartness.PlayerSetVariables(Player)
	CVAR.New(Player, "Smartness", SS.Smartness.Start)
	CVAR.New(Player, "Correct Sentences", 0)
end

// When players GUI is updated

function SS.Smartness.PlayerUpdateGUI(Player)
	Player:SetNetworkedString("Smartness", "Smartness: "..CVAR.Request(Player, "Smartness"))
end


// When player says something

function SS.Smartness.PlayerTypedText(Player, Text)
	Text = " "..Text.." "
	
	local Amount = 0
	local Good = 0
	
	for K, V in pairs(SS.Smartness.Words) do
		if (string.find(Text, " "..V.." ")) then
			Amount = Amount + SS.Smartness.Lose
			
			CVAR.Update(Player, "Smartness", CVAR.Request(Player, "Smartness") - SS.Smartness.Lose)
		end
	end
	
	if (Amount != 0) then
		SS.Smartness.Take(Player, Amount, "Terrible Spelling")
	else
		CVAR.Update(Player, "Correct Sentences", CVAR.Request(Player, "Correct Sentences") + 1)
		
		if (CVAR.Request(Player, "Correct Sentences") >= SS.Smartness.Posts) then
			CVAR.Update(Player, "Correct Sentences", 0)
			
			CVAR.Update(Player, "Smartness", CVAR.Request(Player, "Smartness") + SS.Smartness.Gain)
			
			SS.Smartness.Give(Player, SS.Smartness.Gain, SS.Smartness.Posts.." Perfect Posts!")
		end
	end
	
	SS.Smartness.Check(Player)
end

SS.Smartness:Create()